<template>
    <!--tab模块-->
    <view class="orderList">
        <tab @currentTab.user="getCurrentTab" :tabList="tabList"></tab>
        <scroll-view scroll-y="true" class="swiper-item-box" style="height:{{winHeight - 31}}px" bindscrolltolower="onReachBottom">
            <orderItem @currentTab.user="getCurrentTab" @sendOrd.user="sendOrdFun" wx:if="{{!is_empty}}"></orderItem>
            <!--暂无数据显示-->
            <placeholder wx:if="{{is_empty}}" message.sync="暂无发现数据"></placeholder>
        </scroll-view>
        <view class="btn" wx:if="{{way == 0}}">
            <navigator url="/pages/bidList/buyerBidList">
                <view class="button">买家出价列表</view>
            </navigator>
        </view>
        <view class="btn" wx:elif="{{way == 1}}">
            <navigator url="/pages/bidList/sellerBidList" style="border-bottom: 1px solid #f0f0f0">
                <view class="button">出售出价列表</view>
            </navigator>
            <navigator url="/pages/bidList/returnOrderList">
                <view class="button">我的退货</view>
            </navigator>
        </view>
        <modal class="modal" wx:if="{{hiddenmodalput}}" title="物流单号" confirm-text="提交" cancel-text="取消" bindcancel="cancelM" bindconfirm="confirmM">
            <input bindinput='wlCode' type='text' placeholder="请输入物流单号"/>
        </modal>
    </view>
</template>
<script>
    import wepy from 'wepy'
    import tip from '@/utils/tip'
    import Tab from '@/components/tab'
    import {USER_INFO, SYSTEM_INFO} from '@/utils/constant'
    import OrderItem from '../../components/order_item'
    import Placeholder from '../../components/common/placeholder'
    import api from '@/api/api'

    export default class orderList extends wepy.page {
        components = {
            tab: Tab,
            orderItem: OrderItem,
            placeholder: Placeholder
        }
        data = {
            winHeight: 0,
            tabList: [
                '全部',
                '待付款',
                '待卖家发货',
                '卖家已发货',
                '平台鉴定中',
                '平台已发货',
                '已收货',
                '交易失败'
            ],
            orderList: [],
            is_empty: false,
            showLoading: true,
            form: {
                page: 1,
                limit: 10
            },
            page_total: '',
            way: 0,
            hiddenmodalput : false,
            form2:{
                logisticsCode:'SF'
            }
        }

        async getMyOrder(type) {
            let that = this
            const json = await api.listOrderGoods({
                query: that.form
            })
            if (json.data.code == 0) {
                let l = json.data.data
                that.page_total = Math.ceil(l.total / that.form['limit'])
                that.form['page'] = l.page
                if (type) {
                    that.orderList = [...that.orderList, ...l.rows]
                } else {
                    that.orderList = l.rows
                }
                that.$invoke('orderItem', 'eachItem', that.orderList)
                if (that.orderList.length == 0) {
                    //暂无数据
                    that.is_empty = true
                } else {
                    that.is_empty = false
                }
                that.$apply()
            } else {
                tip.error(json.data.msg)
            }
        }

        onLoad(opts) {
            let that = this
            that.orderList = []
            //设置滚动高度
            let systemInfo = wepy.getStorageSync(SYSTEM_INFO)
            let openId = wepy.getStorageSync('userId')
            that.way = opts.way
            if (openId) {
                that.form["params['userId']"] = openId
                that.form["params['seller']"] = parseInt(that.way)
            }
            that.winHeight = systemInfo.windowHeight
            that.$invoke('orderItem','eachFail',that.way)
            that.getMyOrder()
            if(that.way == 0){
                wx.setNavigationBarTitle({
                    title: '我的购买'
                })
            }else if(that.way == 1){
                wx.setNavigationBarTitle({
                    title: '我的出售'
                })
            }
            that.$apply()
        }

        methods = {
            getCurrentTab(cur) {
                const that = this
                that.is_empty = true;
                that.form["page"] = 1;
                that.orderList = []
                if (cur == 0) {
                    delete that.form["params['state']"];
                } else if (cur == 1) {
                    that.form["params['state']"] = 'WAIT_PAY'//待付款
                } else if (cur == 2) {
                    that.form["params['state']"] = 'WAIT_SELLER_SEND'//待卖家发货
                } else if (cur == 3) {
                    that.form["params['state']"] = 'SELLER_SEND'//卖家已发货
                } else if (cur == 4) {
                    that.form["params['state']"] = 'PLATFORM_IDENTIFY'//平台鉴定中
                } else if (cur == 5) {
                    that.form["params['state']"] = 'PLATFORM_SEND'//确认收货
                } else if (cur == 6) {
                    that.form["params['state']"] = 'RECEIVED'//已收货
                } else if (cur == 7) {
                    that.form["params['state']"] = 'FAIL'//交易失败
                }
                that.getMyOrder();
                that.$invoke('orderItem', 'eachFail',that.way);
                that.$invoke('tab', 'changeTab', cur);
                that.$apply()
            },
            sendOrdFun(r){
                const t = this;
                if(r){
                    t.form2['orderId'] = r;
                    t.hiddenmodalput = true;
                    t.$apply();
                }
            },
            cancelM (){
                this.hiddenmodalput = false;
            },
            confirmM () {
                if(this.form2['logisticsNo']){
                    this.orderSellerSendFun();
                }else {
                    tip.error('请输入物流单号');
                }
            },
            wlCode (e) {
                this.form2['logisticsNo'] = e.detail.value;
            }
        }
        // 卖家发货
        async orderSellerSendFun() {
            const t = this;
            const json = await api.orderSellerSend({
                query: t.form2
            });
            if (json.data.code == 0) {
                t.hiddenmodalput = false;
                t.getMyOrder();
                t.$invoke('orderItem', 'eachFail',t.way);
                t.form["params['state']"] = 'SELLER_SEND';
                t.$invoke('tab', 'changeTab', 3);
                tip.success('发货成功！！',2000);
                t.$apply();
            } else {
                tip.error(json.data.msg);
            }
        }
        //加载更多
        onReachBottom() {
            const that = this
            that.showLoading = true
            if (that.page_total > that.form['page']) {
                that.form['page']++
                that.getMyOrder(1)
            } else {
                that.showLoading = false
            }
        }
    }
</script>
<style lang="less">
    .orderList{
        margin-top: 100rpx;
        .btn{
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            box-sizing: border-box;
            padding:6rpx 20rpx;
            .button{
                color: #333;
            }
            background: #ffffff;
        }
        .swiper-tab-pd {
            padding: 0 30rpx;
            background: #fff;
            overflow: hidden;
            .swiper-tab {
                overflow-y: scroll;
            }
            .swiper-tab::-webkit-scrollbar {
                display: none;
            }
        }

        .swiper-tab-order.active {
            color: #ff4856;
            border-bottom: 5rpx solid #ff4856;
        }
        .empty-placeholder {
            padding-top: 180rpx;
            padding-bottom: 60rpx;
        }

    }
</style>
